<?php
	include "../include/protect.php"; 
 

$allowed_image_types = array('image/pjpeg'=>"jpg",'image/jpeg'=>"jpg",'image/jpg'=>"jpg",'image/png'=>"png",'image/x-png'=>"png",'image/gif'=>"gif");
$allowed_image_ext = array_unique($allowed_image_types); // do not change this
$image_ext = "";	// initialise variable, do not change this.
foreach ($allowed_image_ext as $mime_type => $ext) {
    $image_ext.= strtoupper($ext)." ";
}
$uptypes = array('image/jpg', 'image/jpeg','image/png', 'image/pjpeg','image/gif','image/bmp','image/x-png'); 
$max_file_size = MAX_FILE_SIZE ? MAX_FILE_SIZE : 3 * 1024 * 1024  ; //  上传文件大小限制, 单位BYTE
$destination_folder = "uploadfile/"; //上传文件路径
$jpeg_quality = JPEG_QUALITY ? JPEG_QUALITY : 90 ;

$max_width = UPLOAD_CROP_MAX_WIDTH ? UPLOAD_CROP_MAX_WIDTH:500 ;
$max_height = UPLOAD_IMAGE_MAX_HEIGHT ? UPLOAD_IMAGE_MAX_HEIGHT : 400 ;

//缩略图
$targ_w = THUMB_WIDTH ? THUMB_WIDTH : 320 ;
$targ_h = THUMB_HEIGHT ? THUMB_HEIGHT : 200;

$action = $type ;

$cropType = $_POST['cropType'] ? $_POST['cropType'] : $_GET['cropType'];

if($action=='uploadPhoto'){
	uploadPhoto(500,500) ;
}else if($action=='uploadPScreenShots'){
	uploadPhoto(900,800) ;
}else if($action=='crop'){
	if($cropType == 'userFace'){		
		$photo1 = crop(ROOTPATH . $_POST['src'] ,USERFACE_BIG_WIDTH,USERFACE_BIG_HEIGHT) ;
		$photo2 = crop(ROOTPATH . $_POST['src'],USERFACE_SMALL_WIDTH,USERFACE_SMALL_HEIGHT);
		$photo = saveUserFace($photo1['name'],$photo2['name'],$_SESSION['user_id']);
				
		echo('{"status":"OK","photo":"<img src=\"'. $photo['photoBig'].'?'. time().'\" /><img src=\"'.$photo['photoSmall'] .'?'. time() .'\" />"}') ;
	}else{
		$photo = crop(ROOTPATH . $_POST['src'],$targ_w,$targ_h) ;		
		echo('{"status":"OK","photo":"<img src=\"'.$photo['path'].$photo['name'].'\" />"}') ;
	}
	
	// 安全策略
	$srcName = basename($_POST['src']);
	$srcFile = ROOTPATH . TMP_PATH . $srcName ;
	if( file_exists( $srcFile) )
		unlink( $srcFile );

}



function uploadPhoto($max_width=500,$max_height=500){
	global $max_file_size,$destination_folder,$jpeg_quality, $allowed_image_types;
	$file = $_FILES['Filedata'] ;
	$userfile_name = $_FILES['Filedata']['name'];
	$userfile_tmp = $_FILES['Filedata']['tmp_name'];
	$userfile_size = $_FILES['Filedata']['size'];
	$userfile_width = $_FILES['Filedata']['width'];
	$userfile_type = $_FILES['Filedata']['type'];
	$filename = basename($_FILES['Filedata']['name']);
	$file_ext = strtolower(substr($filename, strrpos($filename, '.') + 1));	
	if((!empty($_FILES['Filedata'])) && ($_FILES['Filedata']['error'] == 0)) {
		foreach ($allowed_image_types as $mime_type => $ext) {
			if($file_ext==$ext){
				$error = "";
				break;
			}else{
				$error = "仅支持 <strong>".$image_ext."</strong> 格式<br />";
			}			
		}
		if ($userfile_size > ( $max_file_size )) {
			$error.= "错误。图片大小不能超过 ".$max_file."KB ，更换小图片";
		}
		if(!file_exists($destination_folder)) mkdir($destination_folder);
		$filesName = date('YmdHis',time()).','. floor(microtime()*10000000);	
		$tempFile = $_FILES['Filedata']['tmp_name'];
		$fileExt = getFileExt($_FILES['Filedata']['name']);
		//$targetPath = $_SERVER['DOCUMENT_ROOT'] . $_REQUEST['folder'] . '/';
		$targetPath = ROOTPATH . TMP_PATH ;
		$targetFile =  str_replace('\\\\','\\', $targetPath ) . $filesName  .'.'.$fileExt ;
		$localFilePath = TMP_PATH .  $filesName  .'.'.$fileExt ;
		move_uploaded_file($tempFile,$targetFile);
		 	
		$width = getWidth($targetFile);
		$height = getHeight($targetFile);
		
		if ($width > $max_width || $height > $max_height){			
			$scaleW = $max_width/$width;
			$scaleH = $max_height/$height;			
			$scale = $scaleW  > $scaleH ? $scaleH :   $scaleW;
			$uploaded = resizeImage($targetFile,$width,$height,$scale);			
		}else{
			$scale = 1;
			$uploaded = resizeImage($targetFile,$width,$height,$scale);
		}  
		//$newName =  $_REQUEST['folder'] . '/' .  $filesName . ','. $width .','. $height .'.'.$fileExt ;
		//rename( $localFilePath , $newName );
   		exit($localFilePath);
	}else{
		$error= "您还没有选择照片呢...";
	}

}
 

function crop($photoSrc, $targ_w = 200 ,$targ_h = 200 ){
	global $jpeg_quality;
	$src = $photoSrc;	
	$img_r = imagecreatefromjpeg($src);
	$dst_r = imagecreatetruecolor( $targ_w, $targ_h );

	imagecopyresampled($dst_r,$img_r,0,0,$_POST['x'],$_POST['y'], $targ_w,$targ_h,$_POST['w'],$_POST['h']);

	$imageType = getFileExt($src);
	$name = date('YmdHis',time()) . floor(microtime()*1000).','. $targ_w .','. $targ_h . '.'.$imageType ; 
	$path =  TMP_PATH ;
	$thumb_image_name = ROOTPATH . $path . $name;
	switch($imageType) {
		case "gif":
	  		imagegif($dst_r,$thumb_image_name); 
			break;
      	case "jpeg":
		case "jpg":
	  		imagejpeg($dst_r,$thumb_image_name, $jpeg_quality); 
			break;
		case "png":
			imagepng($dst_r,$thumb_image_name);  
			break;
    }
	return array('path' => str_replace('\\','/',$path) , 'name' => $name);
}



?>